# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: helpers\mailer\sender\smtp.py
__author__ = "Attila Gerendi (Sanyi)"
import smtplib, logging
from typing import Union, List
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from helpers.mailer.sender.base import Sender

logger = logging.getLogger("mailer.smtp")


class SmtpSender(Sender):
    def send(
        self,
        destination,
        from_address,
        subject,
        body_html=None,
        body_txt=None,
        timeout=0,
    ):
        if from_address is None:
            from_address = self.settings["from_address"]
        destination = (
            (",").join(destination)
            if isinstance(destination, (list, tuple))
            else destination
        )
        if self.settings.get("security") == "ssl":
            client = smtplib.SMTP_SSL(self.settings["address"], self.settings["port"])
        else:
            client = smtplib.SMTP(self.settings["address"], self.settings["port"])
        message = MIMEMultipart("alternative")
        message["Subject"] = subject
        message["From"] = from_address
        message["To"] = destination
        if body_txt is not None:
            message.attach(MIMEText(body_txt, "plain", _charset="utf-8"))
        if body_html is not None:
            message.attach(MIMEText(body_html, "html", _charset="utf-8"))
        client.ehlo()
        if self.settings.get("security") == "tls":
            client.starttls()
            client.ehlo()
        if self.settings.get("username"):
            client.login(self.settings.get("username"), self.settings.get("password"))
        result = client.sendmail(from_address, destination, message.as_string())
        client.quit()
        if len(result) != 0:
            raise Exception(result)
